import path from "node:path";
import UniApp from "@dcloudio/vite-plugin-uni";
import UniLayouts from '@uni-helper/vite-plugin-uni-layouts'
import UniPages from '@uni-helper/vite-plugin-uni-pages'
import UniComponents from "@uni-helper/vite-plugin-uni-components";
import { UniUIResolver } from '@uni-helper/vite-plugin-uni-components/resolvers'
import { uniuseAutoImports } from '@uni-helper/uni-use'
import AutoImport from "unplugin-auto-import/vite";
import UnoCSS from 'unocss/vite'
import { defineConfig } from "vite";
import { NutResolver } from "nutui-uniapp";
import { getRootPath } from "./src/utils/devutil";

function r(...paths: string[]) {
  return path.resolve(process.cwd(), ".", ...paths);
}

export default defineConfig({
  resolve: {
    alias: {
      "@": r("src")
    }
  },
  plugins: [
    UniLayouts(),
    UniPages({dts: r(getRootPath(), 'types/uni-pages.d.ts'),}),
    UniComponents({
      dts: "types/components.d.ts",
      dirs: ["src/components"],
      directoryAsNamespace: true,
      collapseSamePrefixes: true,
      resolvers: [
        NutResolver(),
        UniUIResolver()
      ]
    }),
    AutoImport({
      dts: "types/auto-imports.d.ts",
      imports: [
        "vue",
        "uni-app",
        "pinia",
        {
          "nutui-uniapp/composables": [
            "useNotify",
            //"useToast"
          ]
        },
        uniuseAutoImports()
      ]
    }),
    UnoCSS(),
    // @ts-expect-error whatever
    UniApp.default()
  ]
});